package com.kk.Callable的使用;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * the class of Plan1ThreadsAndJUC
 *
 * <p>
 * .
 *
 * @author little_lunatic
 * @date 2023-03-06
 */
public class CallableDemo {
    public static void main(String[] args) throws Exception {
        CallableImpl callable = new CallableImpl();
        FutureTask<String> futureTask = new FutureTask<>(callable);
        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start();
        new Thread(futureTask, "C").start();
        // get会造成阻塞
        String s = futureTask.get();
        System.out.println(s);
    }
}


class CallableImpl implements Callable<String> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    public String call() throws Exception {
        String res = "FAILED";
        try {
//            int i = 1/0;
            res = "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}

